<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="queryParams.doctorName" placeholder="姓名" class="filter-item search-item" />
      <el-input v-model="queryParams.doctorId" placeholder="工号" class="filter-item search-item" />
      <el-input v-model="queryParams.mobile" placeholder="手机号" class="filter-item search-item" />
      <el-button class="filter-item" type="primary" @click="search">
        查询
      </el-button>
      <el-button class="filter-item" type="primary" @click="search">
        刷新
      </el-button>
      <el-button class="filter-item" type="success" @click="reset">
        {{ $t('table.reset') }}
      </el-button>
    </div>

    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      style="width: 100%;"
      @selection-change="onSelectChange"
      @sort-change="sortChange"
    >
      <el-table-column type="selection" align="center" width="40px" />
      <el-table-column label="姓名" prop="doctorName" :show-overflow-tooltip="true" align="center" min-width="120px">
        <template slot-scope="scope">
          <span>{{ scope.row.doctorName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.user.sex')"
        :filters="[{ text: $t('common.sex.male'), value: '1' }, { text: $t('common.sex.female'), value: '2' }, { text: $t('common.sex.secret'), value: '3' }]"
        :filter-method="filterSex"
        class-name="status-col"
      >
        <template slot-scope="{row}">
          <el-tag :type="row.gender | sexFilter">
            {{ transSex(row.gender) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="工号" :show-overflow-tooltip="true" align="center" min-width="150px">
        <template slot-scope="scope">
          <span>{{ scope.row.doctorId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="机构" align="center" min-width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.organization }}</span>
        </template>
      </el-table-column>
      <el-table-column label="科室" prop="clinicName" align="center" min-width="180px" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.clinicName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" min-width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="职称" align="center" min-width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.ranks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" min-width="100px">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">未审核</span>
          <span v-if="scope.row.status == 1">已审核</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.operation')"
        align="center"
        min-width="150px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <i
            v-hasPermission="['user:view']"
            class="el-icon-view table-operation"
            title="审核"
            style="color: #87d068;"
            @click="audit(row)"
          />
          <i
            v-hasPermission="['user:update']"
            class="el-icon-setting table-operation"
            style="color: #2db7f5;"
            @click="edit(row)"
          />
          <i
            v-hasPermission="['user:delete']"
            class="el-icon-delete table-operation"
            style="color: #f50;"
            @click="singleDelete(row)"
          />
          <!-- <el-link v-has-no-permission="['user:view','user:update','user:delete']" class="no-perm">
            {{ $t('tips.noPermission') }}
          </el-link> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pagination.num"
      :limit.sync="pagination.size"
      @pagination="search"
    />
    <user-edit
      ref="edit"
      :dialog-visible="dialog.isVisible"
      :title="dialog.title"
      @success="editSuccess"
      @close="editClose"
    />
    <!-- <user-view ref="view" :dialog-visible="userViewVisible" @close="viewClose" /> -->
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import UserEdit from './Edit'
// import UserView from './View'

export default {
  name: 'UserManage',
  components: {
    Pagination,
    UserEdit
  },
  filters: {
    sexFilter(status) {
      const map = {
        1: 'success',
        2: 'danger',
        3: 'info'
      }
      return map[status]
    },
    statusFilter(status) {
      const map = {
        0: 'danger',
        1: 'success'
      }
      return map[status]
    }
  },
  data() {
    return {
      dialog: {
        isVisible: false,
        title: ''
      },
      userViewVisible: false,
      tableKey: 0,
      loading: false,
      list: null,
      total: 0,
      queryParams: {},
      sort: {},
      selection: [],
      pagination: {
        size: 10,
        num: 1
      }
    }
  },
  computed: {
    currentUser() {
      return this.$store.state.account.user
    }
  },
  created() {
    this.search()
  },
  methods: {
    transSex(sex) {
      switch (sex) {
        case '1':
          return this.$t('common.sex.male')
        case '2':
          return this.$t('common.sex.female')
        default:
          return this.$t('common.sex.secret')
      }
    },
    filterStatus(value, row) {
      return row.status === value
    },
    filterSex(value, row) {
      return row.gender === value
    },
    viewClose() {
      this.userViewVisible = false
    },
    editClose() {
      this.dialog.isVisible = false
      this.search()
    },
    editSuccess() {
      this.search()
    },
    onSelectChange(selection) {
      this.selection = selection
    },
    search() {
      this.fetch({
        ...this.queryParams,
        ...this.sort
      })
    },
    reset() {
      this.queryParams = {}
      this.sort = {}
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    add() {
      this.dialog.title = this.$t('common.add')
      this.dialog.isVisible = true
    },

    singleDelete(row) {
      this.$refs.table.toggleRowSelection(row, true)
      this.batchDelete()
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t('tips.noDataSelected'),
          type: 'warning'
        })
        return
      }
      let contain = false
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        const userIds = []
        this.selection.forEach((u) => {
          if (u.id === this.currentUser.userId) {
            contain = true
            return
          }
          userIds.push(u.id)
        })
        if (contain) {
          this.$message({
            message: this.$t('tips.containCurrentUser'),
            type: 'warning'
          })
          this.clearSelections()
        } else {
          this.delete(userIds)
        }
      }).catch(() => {
        this.clearSelections()
      })
    },
    clearSelections() {
      this.$refs.table.clearSelection()
    },
    audit(rows) {
      // if(rows.status == 0){
      this.$confirm('是否更改审核状态', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$get('/referral/doctor/updateState', {
          id: rows.id
        }).then((r) => {
          this.loading = false
          this.search()
          this.$emit('success')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },

    delete(userIds) {
      console.log(userIds, 'userIds')
      this.loading = true
      this.$delete(`referral/doctor?id=${userIds}`).then(() => {
        this.$message({
          message: this.$t('tips.deleteSuccess'),
          type: 'success'
        })
        this.search()
      })
    },
    edit(row) {
      // let roleId = []
      // if (row.roleId && typeof row.roleId === 'string') {
      //   roleId = row.roleId.split(',')
      //   row.roleId = roleId
      // }
      this.$refs.edit.setUser(row)
      this.dialog.isVisible = true
      // })
    },
    fetch(params) {
      params.pageSize = this.pagination.size
      params.pageNum = this.pagination.num
      this.loading = true
      this.$get('referral/doctor/list', {
        ...params
      }).then((r) => {
        const data = r.data.data
        this.total = data.total
        this.list = data.rows
        this.loading = false
      })
    },
    sortChange(val) {
      this.sort.field = val.prop
      this.sort.order = val.order
      this.search()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
